package com.yunke.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunke.common.result.YunkeResult;
import com.yunke.entity.vo.YunkeCommunityVo;
import com.yunke.service.YunkeCommunityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 管理员社群管理控制器
 * 
 * @author yunke
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/community")
@RequiredArgsConstructor
@Tag(name = "管理员社群管理", description = "管理员社群管理相关接口")
public class YunkeCommunityAdminController {

    private final YunkeCommunityService communityService;

    @GetMapping("/page")
    @SaCheckRole("admin")
    @Operation(summary = "分页查询社群", description = "管理员分页查询社群列表")
    public YunkeResult<Page<YunkeCommunityVo>> getCommunityPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "社群类型") @RequestParam(required = false) Integer communityType,
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "地区") @RequestParam(required = false) String region,
            @Parameter(description = "经度") @RequestParam(required = false) BigDecimal longitude,
            @Parameter(description = "纬度") @RequestParam(required = false) BigDecimal latitude,
            @Parameter(description = "距离范围(米)") @RequestParam(defaultValue = "10000") Integer distance) {
        Page<YunkeCommunityVo> page = communityService.getCommunityPage(current, size, keyword, 
                communityType, categoryId, region, longitude, latitude, distance, null);
        return YunkeResult.success(page);
    }

    @GetMapping("/detail/{communityId}")
    @SaCheckRole("admin")
    @Operation(summary = "获取社群详情", description = "管理员获取社群详细信息")
    public YunkeResult<YunkeCommunityVo> getCommunityDetail(@Parameter(description = "社群ID") @PathVariable Long communityId) {
        YunkeCommunityVo community = communityService.getCommunityDetail(communityId, null);
        return YunkeResult.success(community);
    }

    @PostMapping("/audit")
    @SaCheckRole("admin")
    @Operation(summary = "审核社群", description = "审核社群申请")
    public YunkeResult<Boolean> auditCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId,
            @Parameter(description = "审核状态（1通过 2拒绝）", required = true) @RequestParam Integer auditStatus,
            @Parameter(description = "审核备注") @RequestParam(required = false) String auditRemark) {
        Long adminId = StpUtil.getLoginIdAsLong();
        boolean result = communityService.auditCommunity(communityId, adminId, auditStatus, auditRemark);
        return YunkeResult.success("审核完成", result);
    }

    @PostMapping("/top")
    @SaCheckRole("admin")
    @Operation(summary = "置顶社群", description = "设置社群置顶")
    public YunkeResult<Boolean> topCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId,
            @Parameter(description = "置顶结束时间", required = true) @RequestParam Long topEndTime) {
        Long adminId = StpUtil.getLoginIdAsLong();
        boolean result = communityService.topCommunity(communityId, adminId, topEndTime);
        return YunkeResult.success("社群置顶成功", result);
    }

    @PostMapping("/cancel-top")
    @SaCheckRole("admin")
    @Operation(summary = "取消置顶", description = "取消社群置顶")
    public YunkeResult<Boolean> cancelTopCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId) {
        Long adminId = StpUtil.getLoginIdAsLong();
        boolean result = communityService.cancelTopCommunity(communityId, adminId);
        return YunkeResult.success("已取消置顶", result);
    }

    @PostMapping("/recommend")
    @SaCheckRole("admin")
    @Operation(summary = "推荐社群", description = "设置社群为推荐")
    public YunkeResult<Boolean> recommendCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId) {
        // 这里可以调用设置推荐的方法
        // 暂时返回成功
        return YunkeResult.success("设置推荐成功", true);
    }

    @PostMapping("/cancel-recommend")
    @SaCheckRole("admin")
    @Operation(summary = "取消推荐", description = "取消社群推荐")
    public YunkeResult<Boolean> cancelRecommendCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId) {
        // 这里可以调用取消推荐的方法
        // 暂时返回成功
        return YunkeResult.success("已取消推荐", true);
    }

    @PostMapping("/ban")
    @SaCheckRole("admin")
    @Operation(summary = "禁用社群", description = "禁用社群")
    public YunkeResult<Boolean> banCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId,
            @Parameter(description = "禁用原因") @RequestParam(required = false) String reason) {
        // 这里可以调用禁用社群的方法
        // 暂时返回成功
        return YunkeResult.success("社群已禁用", true);
    }

    @PostMapping("/unban")
    @SaCheckRole("admin")
    @Operation(summary = "解禁社群", description = "解禁社群")
    public YunkeResult<Boolean> unbanCommunity(
            @Parameter(description = "社群ID", required = true) @RequestParam Long communityId) {
        // 这里可以调用解禁社群的方法
        // 暂时返回成功
        return YunkeResult.success("社群已解禁", true);
    }

    @GetMapping("/stats")
    @SaCheckRole("admin")
    @Operation(summary = "社群统计", description = "获取社群整体统计数据")
    public YunkeResult<Object> getCommunityStats() {
        // 这里可以返回社群相关的统计数据
        // 暂时返回空对象
        return YunkeResult.success("获取统计数据成功", new Object());
    }
} 